fh = open(filepath, mode) # returns a file object called a handle
mode description
r Read (default)
w Write; creates a new file (erasing the data for any file with the same name)
x Write; creates a new file, but fails if the file path already exists
a Append to existing file (create the file if it does not already exist)
r+ Read and write
b Add to mode for binary files (i.e., ‘rb’ or ‘wb’)
t Text mode for files (automatically decoding bytes to Unicode). This is the default if not specified. Add t to other modes to use this (i.e., ‘rt’ or ‘xt’)
echo 'line 1\nline 2\nline 3' > temp.txt
cat temp.txt
#  line 1
#  line 2
#  line 3
fh = open("temp.txt", "r")
fh.name
#  'temp.txt'
fh.mode
#  'r'
fh.close()

Using with means you don’t have to explicitly close the file hand.

with open("temp.txt", "r") as fh:
    ...

reading

read

Reads everything by default - returns a string.

with open("temp.txt", "r") as fh:
    out = fh.read()
out
#  'line 1\nline 2\nline 3\n'
print(out)
#  line 1
#  line 2
#  line 3

Specify the number of characters to read

with open("temp.txt", "r") as fh:
    out = fh.read(2)
print(out)
#  li

readline

Reads one line at a time

with open("temp.txt", "r") as fh:
    print(fh.readline(), end='')
    print(fh.readline(), end='')
    print(fh.readline(), end='')
#  line 1
#  line 2
#  line 3

Specify the number of characters to print

with open("temp.txt", "r") as fh:
    print(fh.readline(3))
    print(fh.readline(4))
    print(fh.readline(3))
    print(fh.readline(4))
    print(fh.readline(3))
    print(fh.readline(4))
#  lin
#  e 1
#  
#  lin
#  e 2
#  
#  lin
#  e 3

Alternatively,

with open("temp.txt", "r") as fh:
    for line in fh:
        print(line, end='')
#  line 1
#  line 2
#  line 3

readlines

Reads everything and returns a list of strings (one line per element).

with open("temp.txt", "r") as fh:
    out = fh.readlines()
print(out)
#  ['line 1\n', 'line 2\n', 'line 3\n']

Specify the number of lines to read

with open("temp.txt", "r") as fh:
    out = fh.readlines(2)
print(out)
#  ['line 1\n']

writing

write

with open("temp.txt", "a") as fh:
    fh.write("line 4\n")
    fh.write("line 5\n")
with open("temp.txt", "r") as fh:
    print(fh.read())
#  line 1
#  line 2
#  line 3
#  line 4
#  line 5
lines = ['line 6\n', 'line 7\n']
with open("temp.txt", "a") as fh:
    for line in lines:
        fh.write(line)
with open("temp.txt", "r") as fh:
    print(fh.read())
#  line 1
#  line 2
#  line 3
#  line 4
#  line 5
#  line 6
#  line 7

writelines


copying

with open("temp.txt", "r") as readfile:
    with open("temp2.txt", "w") as writefile:
        for line in readfile:
            writefile.write(line)
with open("temp2.txt", "r") as fh:
    print(fh.read())
#  line 1
#  line 2
#  line 3
#  line 4
#  line 5
#  line 6
#  line 7

misc

fh.seek(offset,reference_point) # the reference points are 0 (the beginning of the file and is default), 1 (the current position of file) and 2 (the end of the file).
fh.tell()
fh.truncate()
fh.next()
fh.flush()